package com.qf.mybatisplus.function.OCR.test;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.speech.AipSpeech;
import org.json.JSONObject;

import java.util.HashMap;

class Recognition {

    private static final String  APP_ID= "10847087";
    private static final String  API_KEY= "GXrruGB4y8j1Lr58sI5FVQst";
    private static final String  SECRET_KEY= "ZtXrFMnwGNN3QKZXzuSTVTshlmV0nQHs";
//    private static final String  APP_ID= "25392376";
//    private static final String  API_KEY= "tGVkmLcWGazMqDBIVE88EqMC";
//    private static final String  SECRET_KEY= "RaKnelZejjAQcl9QVvNpfKOYYl0GCyHP";
    private static  String image;
    private static String url;

    public  void setImage(String image) {
        Recognition.image = image;
    }

    public  void setUrl(String url) {
        Recognition.url = url;
    }

    AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

    /**
     * 识别本地图片
     */
    public void images() {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
        // 设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 传入参数调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("recognize_granularity", "big");
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("vertexes_location", "true");
        options.put("probability", "true");

        //返回数据为Json格式
        JSONObject res = client.basicGeneral(image, new HashMap<>());
//        System.err.println(res);

//        //生成结果带文字位置信息的
//       JSONObject res = client.general(image, new HashMap());
//
//        //识别结果带文字坐标信息
//        JSONObject res = client.accurateGeneral(image, options);
//
//        //识别带生僻字的图片，需要充钱，放弃了~
//      JSONObject res = client.enhancedGeneral(image, options);

        new SplitTools().getImageResult(res);

    }

    /**
     * 识别本地图片
     */
    public void zsbh() {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
        // 设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 传入参数调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("recognize_granularity", "big");
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("vertexes_location", "true");
        options.put("probability", "true");

        //返回数据为Json格式
        JSONObject res = client.basicGeneral(image, new HashMap<>());
//        System.err.println(res);

//        //生成结果带文字位置信息的
//       JSONObject res = client.general(image, new HashMap());
//
//        //识别结果带文字坐标信息
//        JSONObject res = client.accurateGeneral(image, options);
//
//        //识别带生僻字的图片，需要充钱，放弃了~
//      JSONObject res = client.enhancedGeneral(image, options);

        new SplitTools().getZsbhResult(res);
    }

    /**
     * 网络图片识别
     */
    public void urlImage(){
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
        // 设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        HashMap<String, String> options = new HashMap<>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

        // 通用文字识别, 图片参数为远程url图片
        JSONObject res = client.basicGeneralUrl(url, options);
        new SplitTools().getUrlImage(res);
    }

    /**
     * 识别银行卡
     */
    public void bankCard() {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
        // 设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 传入参数调用接口
        HashMap<String, String> options = new HashMap<>();

        // 本地图片路径
//        String image = "images/bankCard.jpg";
        JSONObject res = client.bankcard(image, options);

        System.out.println(res);
        new SplitTools().getBankCard(res);
    }

    /**
     * 识别身份证
     */
    public  void idCard() {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
        // 设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 传入参数调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("detect_direction", "false");
        options.put("detect_risk", "false");

        String idCardSide = "front";

        // 身份证照片路径
        JSONObject res = client.idcard(image, idCardSide, options);
        System.out.println(res);
        //拆分Json数据
        new SplitTools().getIDCard(res);
    }

}
